package com.lnking.lnkingplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.SpecialPlaceMapper;
import com.lnking.lnkingplace.model.dto.recordUser.RecordDetailDto;
import com.lnking.lnkingplace.model.dto.specialPlace.SpecialPlaceDetailDto;
import com.lnking.lnkingplace.model.entity.PlaceInfo;
import com.lnking.lnkingplace.model.entity.SpecialPlace;
import com.lnking.lnkingplace.model.request.specialPlace.SpecialPlaceAddRequest;
import com.lnking.lnkingplace.model.request.specialPlace.SpecialPlaceUpdateRequest;
import com.lnking.lnkingplace.service.ISpecialPlaceService;
import com.lnking.starter.mybatis.service.impl.CustomServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 场所信息表 服务实现类
 * </p>
 *
 * @author sn
 * @since 2022-09-18
 */
@Service
public class SpecialPlaceServiceImpl extends CustomServiceImpl<SpecialPlaceMapper, SpecialPlace> implements ISpecialPlaceService {


    @Resource
    UserUtil userUtil;

    @Override
    public void add(SpecialPlaceAddRequest params) {
        //验证场所名称是否存在
        String placeName = params.getPlaceName();

        SpecialPlace checkName = checkName(placeName);
        Assert.isTrue(null == checkName, "该场所已存在");

        SpecialPlace checkPhone = checkPhone(params.getLinkmanPhone());
        Assert.isTrue(null == checkPhone, "手机号已存在");

        SpecialPlace placeInfo = new SpecialPlace();
        BeanUtils.copyProperties(params, placeInfo);
        placeInfo.setCreateTime(LocalDateTime.now());

        //获取渠道
        String channel = userUtil.getUser().getChannel();
        placeInfo.setChannel(channel);
        this.baseMapper.insert(placeInfo);
    }

    @Override
    public void update(SpecialPlaceUpdateRequest params) {
        SpecialPlace checkName = checkName(params.getPlaceName());
        Assert.isTrue(null == checkName ||
                checkName.getId().equals(params.getId()), "该场所已存在");

        SpecialPlace placeInfo = new SpecialPlace();
        BeanUtils.copyProperties(params, placeInfo);
        placeInfo.setCreateTime(LocalDateTime.now());
        this.baseMapper.updateById(placeInfo);
    }

    private SpecialPlace checkName(String placeName) {
        QueryWrapper<SpecialPlace> query = new QueryWrapper<>();
        query.lambda().eq(SpecialPlace::getPlaceName, placeName);
        SpecialPlace info = this.baseMapper.selectOne(query);
        return info;
    }

    private SpecialPlace checkPhone(String phone) {
        if(phone!=null&& !phone.equals("")){
            QueryWrapper<SpecialPlace> query = new QueryWrapper<>();
            query.lambda().eq(SpecialPlace::getLinkmanPhone, phone);
            SpecialPlace info = this.baseMapper.selectOne(query);
            return info;
        }
        return null;
    }


    @Override
    public SpecialPlaceDetailDto getUserDetailById(String id) {
        return this.baseMapper.getDetail(id);
    }

    @Override
    public SpecialPlace getByName(String placeName) {
        return getBaseMapper().getByName(placeName);
    }
}
